<template>
    <!-- 设备异常上报 -->
     <el-container class="container">
        <!-- 头部导航 -->
        <el-header class="header">
            <el-header class="headerSon">
                <div>
                    <el-icon><Fold /></el-icon>
                    <span>异常管理</span>
                </div>
                <div>
                    <img :src="userPhoto" alt="">
                    <span>{{ userName }}</span>
                </div>
            </el-header>
        </el-header>
        <!-- 底部内容 -->
        <el-container class="content">
            <el-header>
                <!-- 搜索 -->
                <div>
                    <el-input
                        placeholder="设备编号"
                        v-model="DeviceNumber"
                        style="width:185px;height: 25px;margin-right: 10px;"
                    ></el-input>
                    <el-input
                        placeholder="设备地点"
                        v-model="DeviceLocation"
                        style="width:185px;height: 25px;margin-right: 10px;"
                    ></el-input>
                    <el-select placeholder="选择设备状态" v-model="DeviceState" @change="changes">
                        <el-option 
                            v-for="(item,index) in StateArr" 
                            :key="index" 
                            :value="item.value" 
                            :label="item.label"
                        >
                        </el-option>
                    </el-select>
                    <el-button type="primary" @click="goSearch">搜索</el-button>
                </div>
                <!-- 刷新 -->
                <div class="info">
                    <el-button type="info" @click="goInfo"><el-icon><Refresh /></el-icon>刷新</el-button>
                </div>
            </el-header>
            <el-footer>
                <!-- 1304 -->
                <el-table :data="ReportList" border style="width: 100%" >
                    <el-table-column label="设备编号" prop="code"></el-table-column>
                    <el-table-column label="设备小区" prop="community"></el-table-column>
                    <el-table-column label="异常描述" prop="abnormalDescription"></el-table-column>
                    <el-table-column label="上报人信息" prop="reportUsername"></el-table-column>
                    <el-table-column label="上报时间" prop="reportTime"></el-table-column>
                    <el-table-column label="审核人" prop="reviewerUsername"></el-table-column>
                    <el-table-column label="审核时间" prop="reviewerTime"></el-table-column>
                    <el-table-column label="备注" prop="remarks"></el-table-column>
                    <el-table-column label="状态" prop="reportStatus">
                        <template #default="scope">
                            <span :style="{color:scope.row.reportStatus == 0 ? 'red' : (scope.row.reportStatus == 3 ? 'orange' : 'green')}">
                                {{ scope.row.reportStatus == 0 ? '未处理' : (scope.row.reportStatus == 1 ? '已生成工单' : (scope.row.reportStatus == 2 ? '已处理' : '已维修')) }}
                            </span>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作">
                        <template #default="scope">
                            <el-button @click="goDetails(scope.row,'详情')">详情</el-button>
                            <el-button type="primary" @click="goEdit(scope.row,'编辑')">编辑</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <!-- 分页器 -->
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage"
                    :page-sizes="[15]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total"
                    class="pagination"
                    background 
                />
            </el-footer>
        </el-container>
        <!-- 模态框 -->
        <el-dialog v-model="Dialog" :modal="true">
            <template #header>
                <span>{{ flag }}</span>
            </template>
            <el-container>
                <el-form class="box" ref="myForm" :rules="rulse" :model="deviceDetails">
                    <el-row style="padding-left: 1em;">
                        <el-form-item label="设备编号">
                            <el-input disabled v-model="deviceDetails.code"></el-input>
                        </el-form-item>
                        <el-form-item label="设备小区">
                            <el-input disabled v-model="deviceDetails.community"></el-input>
                        </el-form-item>
                    </el-row>
                    <el-row style="padding-left: 1em;">
                        <el-form-item label="异常描述" style="width:100%;">
                            <el-input
                                type="textarea"
                                :disabled="flag == '编辑' ? false : ''"
                                v-model="deviceDetails.abnormalDescription"
                            ></el-input>
                        </el-form-item>
                    </el-row>
                    <el-row>
                        <el-form-item label="上报人信息">
                            <el-input disabled v-model="deviceDetails.reportUsername"></el-input>
                        </el-form-item>
                        <el-form-item label="上报时间">
                            <el-input disabled v-model="deviceDetails.reportTime"></el-input>
                        </el-form-item>
                    </el-row>
                    <el-row style="padding-left: 3em;">
                        <el-form-item label="状态" style="width:100%;" prop="reportStatus">
                            <el-select
                                :disabled="flag == '编辑' ? false : ''"
                                style="width:45%;"
                                v-model="deviceDetails.reportStatus"
                            >
                                <el-option
                                    v-for="item in modifyDeviceStateArr"
                                    :key="item.value"
                                    :value="item.value"
                                    :label="item.label"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-row>
                    <el-row style="padding-left: 1em;">
                        <el-form-item label="异常类型" style="width:100%;" prop="abnormalType">
                            <el-select
                                :disabled="flag == '编辑' ? false : ''"
                                style="width:45%;"
                                v-model="deviceDetails.abnormalType"
                            >
                                <el-option
                                    v-for="(item,index) in abnormalTips"
                                    :ket="index"
                                    :value="item"
                                    :label="item"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-row>
                    <el-row style="padding-left: 2em;" v-if="flag == '详情'">
                        <el-form-item label="审核人">
                            <el-input disabled v-model="deviceDetails.reviewerUsername"></el-input>
                        </el-form-item>
                        <el-form-item label="审核时间">
                            <el-input disabled v-model="deviceDetails.reviewerTime"></el-input>
                        </el-form-item>
                    </el-row>
                    <el-row style="padding-left: 3em;">
                        <el-form-item label="备注" style="width:100%;">
                            <el-input
                                type="textarea"
                                :disabled="flag == '编辑' ? false : ''"
                                v-model="deviceDetails.remarks"
                            ></el-input>
                        </el-form-item>
                    </el-row>
                    <div class="close">
                        <el-button @click="Dialog = false" v-if="flag == '详情'">关闭</el-button>
                        <el-button @click="Dialog = false" v-if="flag == '编辑'">取消</el-button>
                        <el-button @click="goSure(myForm)" v-if="flag == '编辑'" type="primary">确定</el-button>
                    </div>
                </el-form>
            </el-container>
        </el-dialog>
     </el-container>
</template>

<script setup>
import { onMounted, reactive, ref } from 'vue';
import { 
    getAbnormalList,
    getDeviceDetails,
    getAbnormalTips,
    modifyAbnormalReportNews,
} from '@/components/AbnormalMange/AbnormalMange';
import { ElMessage } from 'element-plus';
onMounted(() => {
    //页面已加载就调数据异常上报的接口
    ReportFun()
    getAbnormalTipsFun()
})
let userPhoto = ref("")//用户头像
let userName = ref("用户")//用户名/手机号
let DeviceState = ref('')//设备状态
let StateArr = ref([
    {value:0,label:'未处理'},
    {value:1,label:'已生成工单'},
    {value:2,label:'已处理'},
    {value:3,label:'已维修'}
])
//调搜索框里面数据的接口
let ReportList = ref([])//数据异常上报的Table里面的数据
let currentPage = ref(1)//页码
let pageSize = ref(15)//页号
let total = ref(0)//总数
let DeviceNumber = ref("")//设备编号
let DeviceLocation = ref("")//设备地点
let abnormalListData = ref({
    pageIndex:currentPage.value,
    pageSize:pageSize.value,
    code:"",//设备编号
    community:"",//设备小区
})
let ReportFun = () =>{//调数据异常上报的接口
    getAbnormalList(abnormalListData.value)
    .then(res => {
        if(res.code == 200){
            if(res.dataset.dataset.List.length != 0){
                ReportList.value = res.dataset.dataset.List
                total.value = res.dataset.dataset.totalSize
                // console.log(ReportList.value);
            }else{
                ReportList.value = []
            }
        }
    })

}
//点击去搜索
let changeFlag = ref('')//用来传设备状态的value
let changes = (v) => {
    changeFlag.value = v
}
let goSearch = () => {
    abnormalListData.value.reportStatus = changeFlag.value
    // if(abnormalListData.value.reportStatus == ''){
    //     delete abnormalListData.value.reportStatus
    // }
    abnormalListData.value.code = DeviceNumber.value
    abnormalListData.value.community = DeviceLocation.value
    ReportFun()
}
let abnormalTips = ref([])//异常类型
let getAbnormalTipsFun = async () => {//获取异常工单列表
    let getAbnormalTipsData = {
        pageIndex:1,
        pageSize:1000,
    }
    try{
        const res = await getAbnormalTips(getAbnormalTipsData)
        if(res.code == 200){
            abnormalTips.value = Array.from(new Set(res.dataset.dataset.List.map(item => item.abnormalType))).filter(item => item != '')
        }
    }catch(error){
        console.log(error);
    }
}
let goInfo = () => {//点击刷新
    location.reload()
}
let Dialog = ref(false)//模态框的开关
let flag = ref("")//点击的是详情还是编辑
let id = ref("")//设备ID
let myForm = ref("")
let deviceDetails = ref({//异常设备详情
    code:"",//设备编号
    community:"",//设备小区
    abnormalDescription:"",//异常描述
    reportUsername:'',//上报人信息
    reportTime:"",//上报时间
    reportStatus:"",//状态
    abnormalType:"",//异常类型
    reviewerUsername:"",//审核人信息
    reviewerTime:"",//审核时间(维修时间)
    remarks:"",//备注
})
let goDetails = async(row,v) => {//点击去详情,row---本行数据,v---详情/编辑
    Dialog.value = true
    flag.value = v
    id.value = row.id
    let DeviceDetailsData = {
        pageIndex:1,
        pageSize:10,
        id:id.value
    }
    try{
        const res = await getDeviceDetails(DeviceDetailsData);
        if (res.code == 200) {
            // console.log(res);
            deviceDetails.value = res.dataset.dataset.List[0];
        }
    }catch(error){
        console.log(error);
    }
}
let goEdit = async (row,v) => {//点击去编辑
    Dialog.value = true
    flag.value = v 
    id.value = row.id
    let DeviceDetailsData = {
        pageIndex:1,
        pageSize:10,
        id:id.value
    }
    try{
        const res = await getDeviceDetails(DeviceDetailsData);
        if (res.code == 200) {
            deviceDetails.value = res.dataset.dataset.List[0];
        }
    }catch(error){
        console.log(error);
    }
}
let modifyDeviceStateArr = ref([//设备状态列表(编辑)
    {value:0,label:'未处理'},
    {value:1,label:'处理中'},
    {value:2,label:'已解决'},
])
const rulse = reactive({
    reportStatus:[{required:true,message:'请选择状态',trigger:'change'}],
    abnormalType:[{required:true,message:'请选择异常类型',trigger:'change'}]
})
let goSure = async (formEl) => {//编辑模态框点击确定
    let modifyDeviceStateData=ref({
        abnormalType:deviceDetails.value.abnormalType,
        abnormalDescription:deviceDetails.value.abnormalDescription,
        id:id.value,
        reportStatus:deviceDetails.value.reportStatus,
        remarks:deviceDetails.value.remarks
    })
    await formEl.validate(async valid => {
        if(valid){
            Dialog.value = false
            //调编辑接口
            try{
                const res = await modifyAbnormalReportNews(modifyDeviceStateData.value)
                if(res.code == 200){
                    ElMessage.success('编辑成功')
                    ReportFun()
                }
            }catch(error){
                console.log(error);
            }
        }
    })
}
//分页器
const size = ref('default')
const disabled = ref(false)
const handleSizeChange = (val) => {
  pageSize.value = val; 
  abnormalListData.value.pageSize = val
  ReportFun(abnormalListData.value)
}
const handleCurrentChange = (val) => {
  currentPage.value = val;
  abnormalListData.value.pageIndex = val
  ReportFun(abnormalListData.value)
}
</script>

<style lang="less" scoped>
.container{
    width:100%;
    height:100%;
    .header{
        background: #fff;
        height:80px;
        padding: 0;
        border-bottom: 1px solid  #ccc;
        box-shadow: 0px 1px 2px 0px #ccc;
        .headerSon{
            height:45px;
            display: flex;
            align-items: center;
            border-bottom: 1px solid #ccc;
            justify-content: space-between;
            div{
                display: flex;
                align-items: center;
                span{
                    display: inline-block;
                    padding:0px 10px;
                    font-size:13px;
                }
            }
        }
    }
    .content{
        width:100%;
        box-sizing: border-box;
        padding:20px;
        .el-header{
            width:100%;
            --el-header-padding: 0;
            :deep(.el-input__inner){
                font-size: 12px;
            }
            .el-select{
                width:185px;
                margin-right:25px;
                :deep(.el-select__wrapper){
                    min-height: 25px;
                    span{
                        font-size:12px;
                    }
                    .el-select__placeholder{
                        transform: translateY(-53%);
                    }
                }
            }
            .el-button {
                height: 25px;
                font-size:12px;
                padding: 4px 10px;
            }
            .info{
                margin-top:10px;
                display: flex;
                justify-content: end;
                .el-button{
                    width:60px;
                }
            }
        }
        .el-footer {
            --el-footer-padding: 0;
            margin-top:5px;
            .el-table{
                :deep(.cell){
                    font-size:12px;
                    padding: 0 6px;
                }
                .el-button{
                    height: 23px;
                    font-size: 12px;
                    padding: 2px 10px;
                }
                :deep(.el-table__cell) {
                    padding: 3px 0;
                }
                // :deep(tr.el-table__row)  {
                //     height:35px;
                //     max-height: 35px;
                // }
            }
        }
    }
}
:deep(.el-dialog__header){
    display: inline-block;
}
:deep(.el-dialog){
    color:#363638;
    border-radius:2px;
    --el-dialog-margin-top:10vh;
    min-width:800px;
    .el-container{
        width:100%;
        font-size:12px;
        .box{
            width:100%;
            .el-row{
                width:100%;
                .el-form-item{
                    width:50%;
                    .el-form-item__label::before{
                        content:''
                    }
                    .el-form-item__label{
                        font-weight: 600;
                    }
                    .el-input__wrapper{
                        height:25px;
                    }
                    .el-select__wrapper {
                        min-height: 25px;
                        width:75%;
                    }
                    .el-input{
                        width:75%;
                    }
                    .el-textarea__inner{
                        width:100%;
                    }
                }
            }
            .close{
                width:100%;
                display: flex;
                justify-content: end;
                .el-button{
                    padding: 2px 10px;
                    font-size:12px;
                }
            }
        }
    }
}
.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}
.el-pagination {
    margin-top:5px;
    justify-content: end;
    box-sizing: border-box;
    padding-right:20px;
    font-size: 12px;
    --el-pagination-font-size: 12px;
    --el-pagination-button-width: 28px;
    --el-pagination-button-height: 28px;
    :deep(.el-select__wrapper){
        font-size: 12px;
        min-height: 25px;
    }
    :deep(.el-input__wrapper){
        height: 25px;
        font-size: 12px;
    }
}

</style>